<template>
    <div class="dashboard-container">
        <div v-show="showFlag">
            <my-search :fields="fieldsList" :baseNumber="10" @onSearch="searchForm" @onReset="resetForm" :selectIsAddAll="false" />
            <div class="btn">
                <el-button type="primary" size="mini" @click="switchHandle">统计</el-button>
                <el-button type="primary" size="mini" @click="exportDataHandle">导出</el-button>
            </div>
            <div class="table-list">
                <!-- <div style="padding:7px;" class="kplist">
                <span>优惠税额合计：{{ yhsehj }}</span>
            </div> -->
                <my-table v-loading="loading" ref="myEltable" style="width: 100%" row-key="id" :showsummary="false" :columns="tableColumns" :tableData="tableDatas" :pageNum="queryObj.pageNum" :pageSize="queryObj.pageSize" :totalPage="total" :height="$store.state.app.windowHeight - 240" max-height="660px" :current="false" @onCurrentChange="changepageNo" @onPageSizeChange="changePageSize" highlight-current-row />
            </div>
        </div>
        <div v-show="!showFlag">
            <el-form :model="ruleForm" ref="ruleForm" label-width="20px" class="demo-ruleForm" :inline="true">
                <el-form-item label="">
                    <!-- <el-checkbox-group v-model="ruleForm.type"> -->
                    <el-checkbox label="行业门类" v-model="ruleForm.hyml_mc"></el-checkbox>
                    <el-checkbox label="行业大类" v-model="ruleForm.hydl_mc"></el-checkbox>
                    <el-checkbox label="行业中类" v-model="ruleForm.hyzl_mc"></el-checkbox>
                    <el-checkbox label="行业小类" v-model="ruleForm.hyxl_mc"></el-checkbox>
                    <el-checkbox label="所属地市" v-model="ruleForm.ssds"></el-checkbox>
                    <el-checkbox label="税种" v-model="ruleForm.sz"></el-checkbox>
                    <el-checkbox label="优惠事项" v-model="ruleForm.yhsx"></el-checkbox>
                    <!-- </el-checkbox-group> -->
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" size="mini" @click="statisticsHandle">统计</el-button>
                    <el-button type="primary" size="mini" @click="resetRuleForm">重置</el-button>
                    <el-button type="primary" size="mini" @click="switchHandle">列表</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="tjloading" :data="tjtableData" style="width: 100%" border :height="$store.state.app.windowHeight - 185">
                <el-table-column type="index" label="排序" width="60" align="center" :index="currIndex">
                </el-table-column>
                <el-table-column prop="SSDS" align="center" show-overflow-tooltip v-if="ruleForm.ssds" label="地市">
                </el-table-column>
                <el-table-column prop="SZ" align="center" show-overflow-tooltip v-if="ruleForm.sz" label="税种">
                </el-table-column>
                <el-table-column prop="YHSX" align="center" show-overflow-tooltip v-if="ruleForm.yhsx" label="优惠事项">
                </el-table-column>
                <el-table-column prop="HYML_MC" align="center" show-overflow-tooltip v-if="ruleForm.hyml_mc" label="行业门类">
                </el-table-column>
                <el-table-column prop="HYDL_MC" align="center" show-overflow-tooltip v-if="ruleForm.hydl_mc" label="行业大类">
                </el-table-column>
                <el-table-column prop="HYZL_MC" align="center" show-overflow-tooltip v-if="ruleForm.hyzl_mc" label="行业中类">
                </el-table-column>
                <el-table-column prop="HYXL_MC" align="center" show-overflow-tooltip v-if="ruleForm.hyxl_mc" label="行业小类">
                </el-table-column>
                <el-table-column prop="SL" align="center" show-overflow-tooltip label="户数">
                </el-table-column>
                <el-table-column prop="PERCENT" align="center" show-overflow-tooltip label="优惠/减免金额（万元）">
                </el-table-column>
            </el-table>
        </div>

    </div>
</template>

<script>

import { queryList, queryJsjfmxfxHj } from '@/api/cxtj/jsjfmxfx'
import { hylx } from '@/api/common'
import {
    queryDetailList
} from '@/api/cxtj/zdsygl'
export default {
    name: 'jsjfmxfx',

    data() {
        return {
            loading: false,
            tjloading: false,
            showFlag: true,
            tjtableData:[],
            ruleForm: {
                hyml_mc: false,
                hydl_mc: false,
                hyzl_mc: false,
                hyxl_mc: false,
                ssds: false,
                sz: false,
                yhsx: false,
            },
            fieldsList: [
                {
                    label: '纳税人名称',
                    field: 'qymc',
                    type: 'autocomplete',
                    value: '',
                    options: []
                },
                {
                    label: '纳税人识别号',
                    field: 'nsrsbh',
                    type: 'text',
                    value: ''
                },
                {
                    label: '优惠日期',
                    field: 'date',
                    type: 'daterange', // daterange
                    value: [],
                    options: [
                        {
                            field: 'kssj',
                            value: ''
                        },
                        {
                            field: 'jssj',
                            value: ''
                        }
                    ],
                    change: value => {
                        // this.changxm(value);
                    }
                },
                {
                    label: '所属地市',
                    field: 'ssds',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        console.log(value)
                        this.gethylx(11, "DM_GY_SWJG", "SJSWJG_DM =(SELECT SWJG_DM FROM DM_GY_SWJG WHERE XZQHSZ_DM = '" + value + "')", "swjg_dm")
                    }
                },
                {
                    label: '行业门类',
                    field: 'hyml_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        this.gethylx(5, "dm_gy_hy", "dlbz='Y' and sjhy_dm = '" + value + "'", "hy_dm")
                    }
                },
                {
                    label: '行业大类',
                    field: 'hydl_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        this.gethylx(6, "dm_gy_hy", "zlbz='Y' and sjhy_dm = '" + value + "'", "hy_dm")
                    }
                },
                {
                    label: '行业中类',
                    field: 'hyzl_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],
                    change: (value) => {
                        this.gethylx(7, "dm_gy_hy", "xlbz='Y' and sjhy_dm = '" + value + "'", "hy_dm")
                    }
                },
                {
                    label: '行业小类',
                    field: 'hyxl_mc',
                    type: 'select',
                    value: '',
                    multiple: false,
                    options: [],

                },
                {
                    label: '税种',
                    field: 'zsxmDm',
                    type: 'select',
                    options: [



                        {
                            label: '增值税',
                            value: '10101'
                        },
                        {
                            label: '消费税',
                            value: '10102'
                        },
                        {
                            label: '营业税',
                            value: '10103'
                        },
                        {
                            label: '企业所得税',
                            value: '10104'
                        },
                        {
                            label: '资源税',
                            value: '10107'
                        },
                        {
                            label: '城市维护建设税',
                            value: '10109'
                        },
                        {
                            label: '房产税',
                            value: '10110'
                        },
                        {
                            label: '印花税',
                            value: '10111'
                        },
                        {
                            label: '城镇土地使用税',
                            value: '10112'
                        },
                        {
                            label: '土地增值税',
                            value: '10113'
                        },
                        {
                            label: '车船税',
                            value: '10114'
                        },
                        {
                            label: '车辆购置税',
                            value: '10116'
                        },
                        {
                            label: '耕地占用税',
                            value: '10118'
                        },
                        {
                            label: '契税',
                            value: '10119'
                        },
                        {
                            label: '烟叶税',
                            value: '10120'
                        },
                        {
                            label: '环境保护税',
                            value: '10121'
                        },
                        {
                            label: '其他收入',
                            value: '30799'
                        },
                        {
                            label: '教育费附加',
                            value: '30203'
                        },
                        {
                            label: '国有土地使用权出让收入',
                            value: '30148'
                        },
                        {
                            label: '地方教育附加',
                            value: '30127'
                        },
                        {
                            label: '残疾人就业保障金',
                            value: '30127'
                        },
                    ],
                    value: ''
                },
                {
                    label: '优惠事项',
                    field: 'ssyhlxDm',
                    type: 'text',
                    value: ''
                },



            ],
            //右侧表格开始
            tableColumns: [
                {
                    label: '纳税人名称',
                    prop: 'nsrmc',
                    align: 'center',
                    minWidth: 275,

                },
                {
                    label: '纳税人识别号',
                    prop: 'nsrsbh',
                    align: 'center',
                    minWidth: 180
                },
                {
                    label: '所属地市',
                    prop: 'ssds',
                    align: 'center',

                },
                {
                    label: '行业门类',
                    prop: 'hyml_mc',
                    align: 'center',

                },
                {
                    label: '行业大类',
                    prop: 'hydl_mc',
                    align: 'center',

                },
                {
                    label: '行业中类',
                    prop: 'hyzl_mc',
                    align: 'center',

                },
                {
                    label: '行业小类',
                    prop: 'hyxl_mc',
                    align: 'center',

                },
                {
                    label: '税种',
                    prop: 'zsxmDm',
                    align: 'center',

                },
                {
                    label: '优惠事项',
                    prop: 'ssyhlxDm',
                    align: 'center',

                },
                //  {
                //     label: '减免事项',
                //     prop: 'jmxmxlDm',
                //     align: 'center',

                // },


                {
                    label: '所属日期起',
                    prop: 'skssqq',
                    align: 'center',
                    width: 120,
                },
                {
                    label: '所属日期止',
                    prop: 'skssqz',
                    align: 'center',
                    width: 120,
                },
                {
                    label: '优惠/减免金额(万元)',
                    prop: 'se',
                    align: 'center',
                    minWidth: 150
                },




            ],
            tableDatas: [],
            queryObj: {
                qymc: '', //公司名称
                nsrsbh: '',
                kssj: '',
                jssj: '',
                ssyhlxDm: '',
                zsxmDm: '',


                pageNum: 1,
                pageSize: 20
            },
            total: 0,
            yhsehj: '',
            searchTxt: ''
        }
    },
    watch: {

        fieldsList: {
            handler(newOption, oldOption) {
                this.searchTxt = newOption[0].value
            },
            deep: true
        },
        searchTxt(newOption, oldOption) {
            console.log('1', newOption, oldOption)
            this.getQueryDetailList(newOption)
        }
    },
    mounted() {
        this.gethylx(4, 'dm_gy_hy', "mlbz = 'Y'", "hy_dm")  //获取行业门类
        this.gethylx(3, 'DIM_XZQH', "SJXZQH_DM = '130000' and XZQHJC = '2'", "XZQH_DM")  //获取城市
        // this.getTableData();
    },
    methods: {
        getQueryDetailList(nsrmc) {
            queryDetailList({ nsrmc: nsrmc }).then(res => {
                if (res.code == 200) {
                    this.fieldsList[0].options = res.data.records.map(item => {
                        return { value: item.NSRMC, label: item.NSRMC }
                    })
                }
                console.log(this.fieldsList[0].options)
            })
        },
        searchForm(params) {
            console.log(params, '查询条件');
            this.queryObj.qymc = params.qymc || '';
            this.queryObj.nsrsbh = params.nsrsbh || '';
            this.queryObj.kssj = params.kssj || '';
            this.queryObj.jssj = params.jssj || '';
            this.queryObj.ssyhlxDm = params.ssyhlxDm || '';
            this.queryObj.zsxmDm = params.zsxmDm || '';
            this.getTableData();
        },
        gethylx(index, tableName, where, orderBy) {
            hylx({
                "tableName": tableName,
                "where": where,
                "orderBy": orderBy
                // "tableName": "dm_gy_hy",
                // "where": " mlbz = 'Y'",
                // "orderBy": "hy_dm"
            }).then(res => {
                console.log(res)
                if (res.data.length > 0) {

                    if (index == 2) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.NSRZTMC, label: item.NSRZTMC }
                        })
                    } else if (index == 9) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.XZQHMC, label: item.XZQHMC }
                        })
                    } else if (index == 3) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.XZQH_DM, label: item.XZQHMC }
                        })
                    } else if (index == 11) {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.SWJG_DM, label: item.SWJGMC }
                        })
                    } else {
                        this.fieldsList[index].options = res.data.map(item => {
                            return { value: item.HY_DM, label: item.HYMC }
                        })
                    }
                }
            })

        },
        resetForm() {
            for (let key in this.queryObj) {
                if (key == 'pageNum') {
                    this.queryObj[key] = 1;
                } else if (key == 'pageSize') {
                    this.queryObj[key] = 20;
                } else {
                    this.queryObj[key] = '';
                }
            }
            // this.getTableData();
            this.tableDatas = []
            this.total = 0
            this.yhsehj = ''
        },
        getTableData() {
            if (this.queryObj.qymc == '') {
                this.$message.error('请输入纳税人名称！')
                return false
            }
            this.loading = true
            queryList(this.queryObj).then(res => {
                this.loading = false
                if (res.code == 200) {
                    this.tableDatas = res.data.records
                    this.total = res.data.total
                }
            })
            queryJsjfmxfxHj(this.queryObj).then(res => {
                this.yhsehj = res.data
            })
        },
        changepageNo(currPage) {
            this.queryObj.pageNum = currPage;
            this.getTableData();
        },
        changePageSize(pageSize) {
            this.queryObj.pageSize = pageSize;
            this.getTableData();
        },
   statisticsHandle() {
            this.tjloading = true
            let condition = []
            for (let i in this.ruleForm) {

                if (this.ruleForm[i]) {
                    condition.push(i)
                }
            }

            if (condition.length <= 0) {
                this.$message.error('请选择统计类型！')
                 this.tjloading = false
                return false
            }
            // let condition = []
            let params = {
                condition: condition.join(','),
                qylx_dm: 11,
                sfzb: 'Y'
            }
            console.log(params)

            statistics(params).then(res => {
                 this.tjloading = false
                this.tjtableData = res.data
                console.log(res)
            })
        },
        resetRuleForm() {
            for (let key in this.ruleForm) {
                this.ruleForm[key] = false;
            }
            this.tjtableData = []
        },
        switchHandle() {
            this.showFlag = !this.showFlag
            for (let key in this.ruleForm) {
                this.ruleForm[key] = false;
            }
            this.tjtableData = []
        },
        exportDataHandle() {
            let params = {
                nsrmc: this.queryObj.nsrmc,
                nsrsbh: this.queryObj.nsrsbh,
                nsrzt: this.queryObj.nsrzt,
                hyml_mc: this.queryObj.hyml_mc,
                hydl_mc: this.queryObj.hydl_mc,
                hyzl_mc: this.queryObj.hyzl_mc,
                hyxl_mc: this.queryObj.hyxl_mc,
                zbsfzj: this.queryObj.zbsfzj,
                ssds: this.queryObj.ssds,
                zgswjg_dm: this.queryObj.zgswjg_dm,
                pageNum: this.queryObj.pageNo,
                pageSize: this.queryObj.pageSize,
                qylx_dm: 11,
                sfzb: 'Y'
            }
            exportData(params).then(res => {
                const elink = document.createElement("a");
                elink.download = localStorage.getItem('jmFileName') || '千户集团总部名单.xlsx';
                elink.style.display = "none";
                elink.href = URL.createObjectURL(res);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
            })

        },
        currIndex(index) {
            return index + 1;
        },
    }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
    height: 100%;
    width: 100%;
    background-color: #fff;
    padding: 8px;

    .btn {
        padding: 8px 8px 8px 0;
    }

    .kplist {
        text-align: right;

        span {
            font-size: 14px;
            font-weight: normal;
            color: rgba(0, 0, 0, 0.56);
        }
    }
}
</style>
